﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using KnowledgeNotebook.DAL;
using System.Threading;
using KnowledgeNotebook.Properties;
using KnowledgeNotebook.BaseClass;


namespace KnowledgeNotebook
{
    public partial class frmLogin : Form
    {
        readonly DatabaseOption _dbOption = new DatabaseOption();


        public frmLogin()
        {
            InitializeComponent();
            
        }

        private void chkServerLogin_CheckedChanged(object sender, EventArgs e)
        {

            lblInfo.Text = String.Empty;
            btnLogin.Enabled = false;
            comboSchemas.Items.Clear();

            if (chkServerLogin.Checked)
            {
                txtUserName.Enabled = true;
                txtPassword.Enabled = true; 
            }
            else
            {
                txtUserName.Enabled = false;
                txtPassword.Enabled = false;
            }
            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
            
           // dbOption.CancelConnection();
        }

        private void comboSchemas_Click(object sender, EventArgs e)
        {
            if ((chkWindowsLogin.Checked && txtServer.Text==String.Empty)||(chkServerLogin.Checked && (txtServer.Text==String.Empty || txtUserName.Text==String.Empty || txtPassword.Text==String.Empty)))
            {
                MessageBox.Show(Resources.LoginFieldNotEmpty, Resources.ErrorCaption,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboSchemas.Items.Clear();
            }
            else
            {
                txtUserName.Enabled = false;
   //             txtServer.Enabled = false;
                txtPassword.Enabled = false;
   //             chkWindowsLogin.Enabled = false;
   //             chkServerLogin.Enabled = false;
                proPending.Enabled = true;
                proPending.Visible = true;

                timer.Enabled = true;

                List<string> schemas;
                

                ThreadPool.QueueUserWorkItem(delegate
                {
                   // DatabaseOption dbOption = new DatabaseOption();
                    if (chkWindowsLogin.Checked)
                    {
                        DatabaseOptionBaseClass.ConnectString = "Data Source=" + txtServer.Text + ";Initial Catalog=master" +
                                                 ";Integrated Security=true";

                        schemas = _dbOption.ChangeSchemas();

                    }
                    else
                    {
                       DatabaseOptionBaseClass.ConnectString = "Data Source=" + txtServer.Text + ";Initial Catalog=master" +
                                ";Persist Security Info=True;User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text;
                        schemas = _dbOption.ChangeSchemas();
 
                    }




                    try
                    {
                        BeginInvoke((MethodInvoker)delegate
                        {
                            if (schemas != null)
                            {
                                comboSchemas.Items.Clear();
                                comboSchemas.Items.AddRange(schemas.ToArray());
                                lblInfo.Visible = true;
                                lblInfo.Text = Resources.DatabaseLinkSuccess;
                                lblInfo.ForeColor = Color.Blue;
                                btnLogin.Enabled = true;
                            }
                            else
                            {
                                lblInfo.Visible = true;
                                lblInfo.Text = Resources.DatabaseLinkFailed;
                                lblInfo.ForeColor = Color.Red;
                                btnLogin.Enabled = false;
                            }



                            if (chkServerLogin.Checked)
                            {
                                txtUserName.Enabled = true;
                                txtPassword.Enabled = true;
                            }


                            

                            timer.Enabled = false;
                            proPending.Enabled = false;
                            proPending.Visible = false;


                        });
                    }
                    catch (Exception ex)
                    {
                            btnLogin.Enabled = false;

                            if (chkServerLogin.Checked)
                            {
                                txtUserName.Enabled = true;
                                txtPassword.Enabled = true;
                            }
                        MessageBox.Show(ex.Message);
                    }


                });



            }


        }



        private void timer_Tick(object sender, EventArgs e)
        {
            proPending.Enabled = true;
            proPending.Visible = true;

            proPending.Value += 1;

            if (proPending.Value>=100)
            {
                proPending.Value = 0;
            }

        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (comboSchemas.Text!=String.Empty)
            {
                if (chkWindowsLogin.Checked)
                {
                   DatabaseOptionBaseClass.ConnectString = "Data Source=" + txtServer.Text + ";Initial Catalog="+comboSchemas.Text +
                                                 ";Integrated Security=true";
                    DatabaseOptionBaseClass.IsTrustConnect = true;
                    DatabaseOptionBaseClass.Database = comboSchemas.Text;
                    DatabaseOptionBaseClass.Server = txtServer.Text;



                }
                else
                {
                    DatabaseOptionBaseClass.ConnectString = "Data Source=" + txtServer.Text + ";Initial Catalog=" +comboSchemas.Text+
                               ";Persist Security Info=True;User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text;

                    DatabaseOptionBaseClass.IsTrustConnect = false;
                    DatabaseOptionBaseClass.Database = comboSchemas.Text;
                    DatabaseOptionBaseClass.Server = txtServer.Text;
                    DatabaseOptionBaseClass.UserName = txtUserName.Text;
                    DatabaseOptionBaseClass.Password = txtPassword.Text;
                }

                this.Hide();
                using (frmMain main = new frmMain())
                {
                    main.Database = comboSchemas.Text;
                    main.ShowDialog();
                }
            }
            else
            {
                Tip.Tip.ShowErrorMsg(Resources.ErrorCaption,"请先选择要连接的数据库");
            }
        }

        private void txtServer_TextChanged(object sender, EventArgs e)
        {
            comboSchemas.Items.Clear();
            lblInfo.Text = String.Empty;
            //if (lblInfo.Visible)
            //{
            //  lblInfo.Visible = false;  
            //}
            //else
            //{
            //    lblInfo.Visible = true;
            //}
            btnLogin.Enabled = false;
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            comboSchemas.Items.Clear();
            lblInfo.Text = String.Empty;
            //if (lblInfo.Visible)
            //{
            //    lblInfo.Visible = false;
            //}
            //else
            //{
            //    lblInfo.Visible = true;
            //}
            btnLogin.Enabled = false;
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            comboSchemas.Items.Clear();
            lblInfo.Text = String.Empty;
            //if (lblInfo.Visible)
            //{
            //    lblInfo.Visible = false;
            //}
            //else
            //{
            //    lblInfo.Visible = true;
            //}
            
            btnLogin.Enabled = false;
        }



    }
}
